/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The software in this package is published under the terms of the GPL v2.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE-GPL.txt file.
 */

package org.cemantika.aspects;

import org.aspectj.lang.annotation.SuppressAjWarnings;
import org.cemantika.contextsource.ContextSourceAdapter;
import org.cemantika.management.ContextManager;
import org.cemantika.metamodel.structure.AcquisitionAssociation;
import org.cemantika.metamodel.structure.ContextSource;
import org.cemantika.util.CemantikaLogger;

/**
 * ContextualElementAcquisition represents the aspect that implements the context source
 * acquisition.
 * 
 * @author andersonfellipe
 */
@SuppressWarnings("unchecked")
public aspect ContextualElementAcquisition {

    /**
     * Represents the moment when some attribute with the {@link AcquisitionAssociation} annotation
     * into a class with the {@link ContextSource} annotation receives a value.
     * 
     * @param value the object with the new value.
     * @param acquisitionAssociation the object of the annotation {@link AcquisitionAssociation}.
     */
    pointcut contextualInformationAcquisition(Object contextSource, Object value, AcquisitionAssociation acquisitionAssociation): 
        @within(ContextSource) &&
        target(contextSource) &&
        set(* *) && 
        args(value) && 
        @annotation(acquisitionAssociation);

    /**
     * Represents the around advice that calls the updateContextualElement method of the class that
     * implements {@link ContextSourceAdapter} and has this context source registered.
     * 
     * @param value the object of the new value.
     * @param acquisitionAssociation the object of the annotation {@link AcquisitionAssociation}.
     */
    @SuppressWarnings("restriction")
    @SuppressAjWarnings( {"adviceDidNotMatch"})
    after(Object contextSource, Object value, AcquisitionAssociation acquisitionAssociation): 
        contextualInformationAcquisition(contextSource, value, acquisitionAssociation) {

        String contextSourceType = contextSource.getClass().getSimpleName();
        ContextManager contextManager = ContextManager.getInstance();
        ContextSourceAdapter contextSourceAdapter =
                (ContextSourceAdapter) contextManager.getContextSourceAdapter(acquisitionAssociation.element(),
                        contextSourceType);
        if (contextSourceAdapter != null) {
            contextManager.addContextualElementAcquired(acquisitionAssociation.element());
            try {
                contextSourceAdapter.updateContextualElement(value);
                CemantikaLogger.logger.info("** Contextual information updated: " + acquisitionAssociation.element());
            } catch (ClassCastException e) {
                CemantikaLogger.logger.error("** The type of contextual information '"
                        + acquisitionAssociation.element() + "' is diferent from the contextual element expected in '"
                        + contextSourceAdapter.getClass().getSimpleName() + "'.");
                e.printStackTrace();
            }
        } else {
            CemantikaLogger.logger
                    .warn("** Contextual information was NOT updated, because there is NOT a context source adapter registered to the element '"
                            + acquisitionAssociation.element() + "' and type '" + contextSourceType + "'.");
        }
    }
}
